<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <!--Setting-->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta http-equiv="Cache-Control" content="no-siteapp">
    <meta http-equiv="Cache-Control" content="no-transform">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta name="apple-mobile-web-app-capable" content="efinedxq">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no,email=no,adress=no">
    <meta name="browsermode" content="application">
    <meta name="screen-orientation" content="portrait">
    <link rel="dns-prefetch" href="https://efinedxq.github.io">
    <!--SEO-->





<meta name="robots" content="all" />
<meta name="google" content="all" />
<meta name="googlebot" content="all" />
<meta name="verify" content="all" />
    <!--Title-->


<title>清华-06-手机键盘 | efinedxq</title>


    <link rel="alternate" href="/atom.xml" title="efinedxq" type="application/atom+xml">


    <link rel="icon" href="/favicon.ico">

    



<link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.4/css/bootstrap.min.css?rev=3.3.4">
<link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="/css/style.css?rev=@@hash">




    
    <div class="hide">
    	<script src="https://s4.cnzz.com/z_stat.php?id=1263868967&web_id=1263868967" language="JavaScript"></script>
    </div>






    
</head>


<!--[if lte IE 8]>
<style>
    html{ font-size: 1em }
</style>
<![endif]-->
<!--[if lte IE 9]>
<div style="ie">你使用的浏览器版本过低，为了你更好的阅读体验，请更新浏览器的版本或者使用其他现代浏览器，比如Chrome、Firefox、Safari等。</div>
<![endif]-->

<body>
    <header class="main-header"  style="background-image:url(/img/banner2.jpg)"  >
    <div class="main-header-box">
        <a class="header-avatar" href="/" title="">
            <img src="/img/author.jpg" alt="logo头像">
        </a>
        <div class="branding">
        	<!--<h2 class="text-hide">Snippet主题,从未如此简单有趣</h2>-->
            
                <h1> 往者不可谏,来者犹可追。 </h1>
             
    	</div>
    </div>
</header>
    <nav class="main-navigation">
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <div class="navbar-header"><span class="nav-toggle-button collapsed" data-toggle="collapse" data-target="#main-menu" id="mnav">
                    <span class="sr-only">Toggle navigation</span>
                    <i class="fa fa-bars"></i>
                    </span>
                </div>
                <div class="collapse navbar-collapse" id="main-menu">
                    <ul class="menu">
                        
                            <li role="presentation"><a href="/"><i class="fa fa-fw "></i>首页</a>
                            </li>
                        
                            <li role="presentation"><a href="/categories/"><i class="fa fa-fw "></i>前端</a>
                            </li>
                        
                            <li role="presentation"><a href="/categories/"><i class="fa fa-fw "></i>后端</a>
                            </li>
                        
                            <li role="presentation"><a href="/categories/"><i class="fa fa-fw "></i>工具</a>
                            </li>
                        
                            <li role="presentation"><a href="/categories/题解/"><i class="fa fa-fw "></i>题解</a>
                            </li>
                        
                            <li role="presentation"><a href="/categories/"><i class="fa fa-fw "></i>资源</a>
                            </li>
                        
                    </ul>
                </div>
            </div>
        </div>
    </div>
</nav>
    <section class="content-wrap">
        <div class="container">
            <div class="row">
                <main class="col-md-8 main-content m-post">
                    <p id="process"></p>
<article class="post">
    <div class="post-head">
        <h1 id="清华-06-手机键盘">
            
	            清华-06-手机键盘
            
        </h1>
        <div class="post-meta">
    
    
    <span class="categories-meta fa-wrap">
        <i class="fa fa-folder-open-o"></i>
        <span>清华</span>
    </span>
    
    
    <span class="fa-wrap">
        <i class="fa fa-tags"></i>
        <span class="tags-meta">
            
            字符串
            
        </span>
    </span>
    
    
    <span class="fa-wrap">
        <i class="fa fa-clock-o"></i>
        <span class="date-meta">2018/02/02</span>
    </span>
</div>

            
            
    </div>
    
    <div class="post-body post-content">
        <h3 id="题目描述"><a href="#题目描述" class="headerlink" title="题目描述"></a>题目描述</h3><p>按照手机键盘输入字母的方式，计算所花费的时间 如：a,b,c都在“1”键上，输入a只需要按一次，输入c需要连续按三次。 如果连续两个字符不在同一个按键上，则可直接按，如：ad需要按两下，kz需要按6下 如果连续两字符在同一个按键上，则两个按键之间需要等一段时间，如ac，在按了a之后，需要等一会儿才能按c。 现在假设每按一次需要花费一个时间段，等待时间需要花费两个时间段。 现在给出一串字符，需要计算出它所需要花费的时间。</p>
<hr>
<h3 id="输入描述"><a href="#输入描述" class="headerlink" title="输入描述:"></a>输入描述:</h3><p>一个长度不大于100的字符串，其中只有手机按键上有的小写字母</p>
<hr>
<h3 id="输出描述"><a href="#输出描述" class="headerlink" title="输出描述:"></a>输出描述:</h3><p>输入可能包括多组数据，对于每组数据，输出按出Input所给字符串所需要的时间</p>
<hr>
<h3 id="示例1"><a href="#示例1" class="headerlink" title="示例1"></a>示例1</h3><p>输入</p>
<blockquote>
<p>bob<br>www</p>
</blockquote>
<p>输出</p>
<blockquote>
<p>7<br>7</p>
</blockquote>
<hr>
<h3 id="分析："><a href="#分析：" class="headerlink" title="分析："></a>分析：</h3><ol>
<li>记录下上次是哪个按键。</li>
<li>找到该字母在按键的哪个位置。</li>
</ol>
<hr>
<h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//好笨的方法。</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">solution</span><span class="params">(<span class="built_in">string</span> s)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">int</span> preK = <span class="number">0</span>, ans = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; s.length(); i ++) &#123;</span><br><span class="line">        <span class="keyword">if</span>(s[i]&gt;=<span class="string">'a'</span>&amp;&amp;s[i]&lt;=<span class="string">'c'</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span>(preK==<span class="number">2</span>) ans+=<span class="number">2</span>;</span><br><span class="line">            <span class="keyword">if</span>(s[i]==<span class="string">'a'</span>) ans++;</span><br><span class="line">            <span class="keyword">else</span> <span class="keyword">if</span>(s[i]==<span class="string">'b'</span>) ans +=<span class="number">2</span>;</span><br><span class="line">            <span class="keyword">else</span> ans+=<span class="number">3</span>;</span><br><span class="line">            preK = <span class="number">2</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span>(s[i]&gt;=<span class="string">'d'</span>&amp;&amp;s[i]&lt;=<span class="string">'f'</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span>(preK==<span class="number">3</span>) ans+=<span class="number">2</span>;</span><br><span class="line">            <span class="keyword">if</span>(s[i]==<span class="string">'d'</span>) ans++;</span><br><span class="line">            <span class="keyword">else</span> <span class="keyword">if</span>(s[i]==<span class="string">'e'</span>) ans +=<span class="number">2</span>;</span><br><span class="line">            <span class="keyword">else</span> ans+=<span class="number">3</span>;</span><br><span class="line">            preK = <span class="number">3</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span>(s[i]&gt;=<span class="string">'g'</span>&amp;&amp;s[i]&lt;=<span class="string">'i'</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span>(preK==<span class="number">4</span>) ans+=<span class="number">2</span>;</span><br><span class="line">            <span class="keyword">if</span>(s[i]==<span class="string">'g'</span>) ans++;</span><br><span class="line">            <span class="keyword">else</span> <span class="keyword">if</span>(s[i]==<span class="string">'h'</span>) ans +=<span class="number">2</span>;</span><br><span class="line">            <span class="keyword">else</span> ans+=<span class="number">3</span>;</span><br><span class="line">            preK = <span class="number">4</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span>(s[i]&gt;=<span class="string">'j'</span>&amp;&amp;s[i]&lt;=<span class="string">'l'</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span>(preK==<span class="number">5</span>) ans+=<span class="number">2</span>;</span><br><span class="line">            <span class="keyword">if</span>(s[i]==<span class="string">'j'</span>) ans++;</span><br><span class="line">            <span class="keyword">else</span> <span class="keyword">if</span>(s[i]==<span class="string">'k'</span>) ans +=<span class="number">2</span>;</span><br><span class="line">            <span class="keyword">else</span> ans+=<span class="number">3</span>;</span><br><span class="line">            preK = <span class="number">5</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span>(s[i]&gt;=<span class="string">'m'</span>&amp;&amp;s[i]&lt;=<span class="string">'o'</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span>(preK==<span class="number">6</span>) ans+=<span class="number">2</span>;</span><br><span class="line">            <span class="keyword">if</span>(s[i]==<span class="string">'m'</span>) ans++;</span><br><span class="line">            <span class="keyword">else</span> <span class="keyword">if</span>(s[i]==<span class="string">'n'</span>) ans +=<span class="number">2</span>;</span><br><span class="line">            <span class="keyword">else</span> ans+=<span class="number">3</span>;</span><br><span class="line">            preK = <span class="number">6</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span>(s[i]&gt;=<span class="string">'p'</span>&amp;&amp;s[i]&lt;=<span class="string">'s'</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span>(preK==<span class="number">7</span>) ans+=<span class="number">2</span>;</span><br><span class="line">            <span class="keyword">if</span>(s[i]==<span class="string">'p'</span>) ans++;</span><br><span class="line">            <span class="keyword">else</span> <span class="keyword">if</span>(s[i]==<span class="string">'q'</span>) ans +=<span class="number">2</span>;</span><br><span class="line">            <span class="keyword">else</span> <span class="keyword">if</span>(s[i]==<span class="string">'r'</span>) ans +=<span class="number">3</span>;</span><br><span class="line">            <span class="keyword">else</span> ans+=<span class="number">4</span>;</span><br><span class="line">            preK = <span class="number">7</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span>(s[i]&gt;=<span class="string">'t'</span>&amp;&amp;s[i]&lt;=<span class="string">'v'</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span>(preK==<span class="number">8</span>) ans+=<span class="number">2</span>;</span><br><span class="line">            <span class="keyword">if</span>(s[i]==<span class="string">'t'</span>) ans++;</span><br><span class="line">            <span class="keyword">else</span> <span class="keyword">if</span>(s[i]==<span class="string">'u'</span>) ans +=<span class="number">2</span>;</span><br><span class="line">            <span class="keyword">else</span> ans+=<span class="number">3</span>;</span><br><span class="line">            preK = <span class="number">8</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">if</span>(preK==<span class="number">9</span>) ans+=<span class="number">2</span>;</span><br><span class="line">            <span class="keyword">if</span>(s[i]==<span class="string">'w'</span>) ans++;</span><br><span class="line">            <span class="keyword">else</span> <span class="keyword">if</span>(s[i]==<span class="string">'x'</span>) ans +=<span class="number">2</span>;</span><br><span class="line">            <span class="keyword">else</span> <span class="keyword">if</span>(s[i]==<span class="string">'y'</span>) ans +=<span class="number">3</span>;</span><br><span class="line">            <span class="keyword">else</span> ans+=<span class="number">4</span>;</span><br><span class="line">            preK = <span class="number">9</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">   <span class="keyword">return</span> ans;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

    </div>

    <div class="post-footer">   
        <div>
            
                转载声明：商业转载请联系作者获得授权,非商业转载请注明出处 © <a href="" target="_blank">efinedxq</a>
            
        </div>
        <div>
            
        </div>  
    </div>
</article>

<div class="article-nav prev-next-wrap clearfix">
    
        <a href="/题解/牛客/清华/清华-07-整数拆分/" class="pre-post btn btn-default"><i class="fa fa-angle-left fa-fw"></i>上一篇</a>
    
    
        <a href="/题解/牛客/清华/清华-05-质因数的个数/" class="next-post btn btn-default">下一篇<i class="fa fa-angle-right fa-fw"></i></a>
    
</div>


    <div id="comments">
        
	
<div id="lv-container" data-id="city" data-uid="MTAyMC8zMzA1MS85NjEz">
  <script type="text/javascript">
     (function(d, s) {
         var j, e = d.getElementsByTagName(s)[0];
         if (typeof LivereTower === 'function') { return; }
         j = d.createElement(s);
         j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
         j.async = true;
         e.parentNode.insertBefore(j, e);
     })(document, 'script');
  </script>
</div>


    </div>





                </main>
                
    <aside class="col-md-4 sidebar">
        
        
    <div class="widget">    
        <h3 class="title">搜索</h3>
        <div id="search-form">
            <div id="result-mask" class="hide"></div>
            <div class="search-area">
                
                    <input id="search-key" type="search" autocomplete="off" placeholder="搜点什么呢?">
                    <button type="button" class="search-form-submit" id="search-local">站内搜索</button>
                
                
            </div>
            <div id="result-wrap" class="hide">
                <div id="search-result"></div>
            </div>
            <div class="hide">
                <template id="search-tpl">
                    <div class="item">
                        <a href="/{path}" title="{title}">
                            <div class="title">{title}</div>
                            <div class="content">{content}</div>
                        </a>
                    </div>
                </template>
            </div>
        </div>
    </div>

        
        
  <div class="widget">
    <h3 class="title">标签云</h3>
    <div class="content tag-cloud">
        <a href="/tags/leetcode/" style="font-size: 16.67px;">leetcode</a> <a href="/tags/map/" style="font-size: 10px;">map</a> <a href="/tags/中间数/" style="font-size: 10px;">中间数</a> <a href="/tags/二叉排序树/" style="font-size: 10px;">二叉排序树</a> <a href="/tags/二叉树/" style="font-size: 10px;">二叉树</a> <a href="/tags/二进制/" style="font-size: 10px;">二进制</a> <a href="/tags/优先队列/" style="font-size: 13.33px;">优先队列</a> <a href="/tags/动态规划/" style="font-size: 10px;">动态规划</a> <a href="/tags/博客配置/" style="font-size: 10px;">博客配置</a> <a href="/tags/反序/" style="font-size: 10px;">反序</a> <a href="/tags/哈夫曼树/" style="font-size: 10px;">哈夫曼树</a> <a href="/tags/复数/" style="font-size: 10px;">复数</a> <a href="/tags/字符串/" style="font-size: 16.67px;">字符串</a> <a href="/tags/指针/" style="font-size: 10px;">指针</a> <a href="/tags/排序/" style="font-size: 13.33px;">排序</a> <a href="/tags/数学/" style="font-size: 20px;">数学</a> <a href="/tags/数组/" style="font-size: 20px;">数组</a> <a href="/tags/暴力/" style="font-size: 10px;">暴力</a> <a href="/tags/最长子串/" style="font-size: 10px;">最长子串</a> <a href="/tags/枚举/" style="font-size: 13.33px;">枚举</a> <a href="/tags/查找/" style="font-size: 13.33px;">查找</a> <a href="/tags/栈/" style="font-size: 10px;">栈</a> <a href="/tags/树遍历/" style="font-size: 10px;">树遍历</a> <a href="/tags/比较查找/" style="font-size: 10px;">比较查找</a> <a href="/tags/矩阵/" style="font-size: 10px;">矩阵</a> <a href="/tags/类排序/" style="font-size: 10px;">类排序</a> <a href="/tags/约数/" style="font-size: 10px;">约数</a> <a href="/tags/结构体/" style="font-size: 13.33px;">结构体</a> <a href="/tags/质因数/" style="font-size: 10px;">质因数</a> <a href="/tags/递归/" style="font-size: 10px;">递归</a> <a href="/tags/链表/" style="font-size: 10px;">链表</a>
    </div>
  </div>


        
        
    <div class="widget">
        <h3 class="title">分类</h3>
        <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/hexo/"><i class="fa" aria-hidden="true">hexo</i></a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/题解/"><i class="fa" aria-hidden="true">题解</i></a><span class="category-list-count">24</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/题解/LeetCode/"><i class="fa" aria-hidden="true">LeetCode</i></a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/题解/牛客/"><i class="fa" aria-hidden="true">牛客</i></a><span class="category-list-count">20</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/题解/牛客/北邮/"><i class="fa" aria-hidden="true">北邮</i></a><span class="category-list-count">13</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/题解/牛客/清华/"><i class="fa" aria-hidden="true">清华</i></a><span class="category-list-count">7</span></li></ul></li></ul></li></ul>
    </div>


        
        
    <div class="widget">
      <h3 class="title">归档</h3>
        <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/02/"><i class="fa" aria-hidden="true">二月 2018</i></a><span class="archive-list-count">8</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/01/"><i class="fa" aria-hidden="true">一月 2018</i></a><span class="archive-list-count">20</span></li></ul>
    </div>


        
        
    <div class="widget">
        <h3 class="title">友链</h3>
        <div class="content friends-link">
        
            <a href="http://blog.csdn.net/efine_dxq" class="fa" target="_blank">个人博客</a>
        
        </div>
    </div>


        
        
    <div class="widget">
      <h3 class="title">社交</h3> 
        <div class="content social">
            
	            <a href="//github.com/efinedxq" rel="external nofollow" title="Github" target="_blank">
			    	<i class="git fa fa-git"></i>
			    </a>
            
	            <a href="mailto:dangxq1008@163.com" rel="external nofollow" title="邮箱" target="_blank">
			    	<i class="envelope-o fa fa-envelope-o"></i>
			    </a>
            
	            <a href="/" rel="external nofollow" title="联系QQ" target="_blank">
			    	<i class="qq fa fa-qq"></i>
			    </a>
            
	            <a href="/" rel="external nofollow" title="微博" target="_blank">
			    	<i class="weibo fa fa-weibo"></i>
			    </a>
            
	            <a href="/" rel="external nofollow" title="QQ群" target="_blank">
			    	<i class="users fa fa-users"></i>
			    </a>
            
	            <a href="/atom.xml" rel="external nofollow" title="RSS" target="_blank">
			    	<i class="feed fa fa-feed"></i>
			    </a>
            
        </div>
    </div>


        
        
    <div class="widget notification">
        <h3 class="title">网站公告</h3>
        <div>
            <p>主题Snippet v1.2.0版本已经上线！欢迎更新~ <br/>
主题下载：<a href="https://github.com/shenliyang/hexo-theme-snippet" title="fork me" target="_blank">Snippet主题</a> <br/>
<hr/>接受贡献，包括不限于提交问题与需求，修复代码。欢迎Pull Request<br/>支持主题：<a href="https://github.com/shenliyang/hexo-theme-snippet/stargazers">Star一下</a>
</p>
        </div>
    </div>

        
    </aside>

            </div>
        </div>
    </section>
    <footer class="main-footer">
    <div class="container">
        <div class="row">
        </div>
    </div>
</footer>

<a id="back-to-top" class="hide">
	<i class="fa fa-chevron-up"></i>
</a>




    <div class="copyright">
    <div class="container">
        <div class="row">
            <div class="col-sm-12"> 
                <span>Copyright &copy; 2017
                </span> | 
                <span>
                    Powered by <a href="//hexo.io" class="copyright-links" target="_blank" rel="nofollow">Hexo</a>
                </span> | 
                <span>
                    Theme by <a href="//github.com/shenliyang/hexo-theme-snippet.git" class="copyright-links" target="_blank" rel="nofollow">Snippet</a>
                </span>
            </div>
        </div>
    </div>
</div>



	<script src="/js/search.js?rev=@@hash"></script>


<script src="/js/app.js?rev=@@hash"></script>


</body>
</html>